/* 
 *  Syntelos XB
 *  Copyright (C) 2006 John Pritchard.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307 USA
 */

package syntelos.xb.op;

/**
 * <p> JVM instruction <code>0xbc newarray</code>. </p>
 */
public final class Newarray
    extends syntelos.xb.Op.Operand
{
    public final static syntelos.iou.dom.Name NAME = new syntelos.iou.dom.impl.Name(ELEMENT_NODE,"http://www.syntelos.org/xb/op","xop:newarray");
    public final static int code = CODE_NEWARRAY;
    public final static java.lang.String name = "newarray";
    public final static int size_operands = 1;
    public final static int count_stack_pop = 1;
    public final static int count_stack_push = 1;

    /**
     * Valid operand values are members of this set.
     */
    public final static int T_BOOLEAN = 4;
    public final static int T_CHAR = 5;
    public final static int T_FLOAT = 6;
    public final static int T_DOUBLE = 7;
    public final static int T_BYTE = 8;
    public final static int T_SHORT = 9;
    public final static int T_INT = 10;
    public final static int T_LONG = 11;

    private final static syntelos.iou.objmap dict = new syntelos.iou.objmap();
    static {
	dict.put("boolean",new java.lang.Integer(T_BOOLEAN));
	dict.put("char",new java.lang.Integer(T_CHAR));
	dict.put("float",new java.lang.Integer(T_FLOAT));
	dict.put("double",new java.lang.Integer(T_DOUBLE));
	dict.put("byte",new java.lang.Integer(T_BYTE));
	dict.put("short",new java.lang.Integer(T_SHORT));
	dict.put("int",new java.lang.Integer(T_INT));
	dict.put("long",new java.lang.Integer(T_LONG));
    }
    public final static int TypeFromString(String type){
	java.lang.Integer re = (java.lang.Integer)dict.get(type);
	if (null != re)
	    return re.intValue();
	else
	    throw new syntelos.xb.Error.Argument(type);
    }
    public final static String TypeToString(int type){
	switch (type){
	case T_BOOLEAN:
	    return "boolean";
	case T_CHAR:
	    return "char";
	case T_FLOAT:
	    return "float";
	case T_DOUBLE:
	    return "double";
	case T_BYTE:
	    return "byte";
	case T_SHORT:
	    return "short";
	case T_INT:
	    return "int";
	case T_LONG:
	    return "long";
	default:
	    throw new syntelos.xb.Error.Argument(java.lang.String.valueOf(type));
	}
    }


    public Newarray(org.w3c.dom.Document doc, syntelos.iou.dom.Name type){
        super(doc,type);
    }
    public Newarray(){
        super(NAME,false);
    }
    public Newarray(boolean shared){
        super(NAME,true);
    }

    public void setOperand(int value){
	switch (value){
	case T_BOOLEAN:
	case T_CHAR:
	case T_FLOAT:
	case T_DOUBLE:
	case T_BYTE:
	case T_SHORT:
	case T_INT:
	case T_LONG:
	    super.setOperand(value);
	    return;
	default:
	    throw new syntelos.xb.Error.Argument(java.lang.String.valueOf(value));
	}
    }
    public int getCode(){
        return code;
    }
    public java.lang.String getName(){
        return name;
    }
    public int sizeOperands(){
        return size_operands;
    }
    public int countStackPop(){
        return count_stack_pop;
    }
    public int countStackPush(){
        return count_stack_push;
    }
    public void readCF(syntelos.xb.Class clas, syntelos.xb.Method meth, syntelos.xb.Op.Reader in)
        throws java.io.IOException
    {
        if (this.setPC(in)){
            if (0 < size_operands){
                byte[] cf = new byte[size_operands];
                in.readFully(cf,0,size_operands);
                this.setCF(cf);
		this.setOperand(this.getCFIntU1(0));
            }
        }
        else
            throw new syntelos.xb.Error.Bug();
    }
    public void writeCF(syntelos.xb.Class clas, syntelos.xb.Method meth, syntelos.xb.Op.Writer out)
        throws java.io.IOException
    {
        if (this.setPC(out)){
            byte[] cf = this.getCF();
	    this.setCFIntU1(0,this.getOperand());
	    out.write(cf);
        }
        else
            throw new syntelos.xb.Error.Bug();
    }
}
